﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TVL02
    {
   
        public TVL02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVERSART();
            this.DataContext = MV;
             InitializeComponent();
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster != null)
            {
                KEY = dwMaster["VVERSART"].ToString() + dwMaster["VSPED"].ToString();
            }
            else
            {
                KEY = "";
            }
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
         
            dwData = TBMain.NewRow();
            dwData["VMANDANT"] = app.Mandant;
            dwData["VVERSART"] = ART;
            dwData["VBEZ"] = DES;
            SResult = TMV.QVER_2(dwData);
            MergeData(SResult, true);
        }
         
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                SART = "";
                VSPED = "";
                return;
            }
            if (SART.Length < 3)
                return;
            if (VSPED.Length < 3)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["VVERSART"] = SART;
            dwData["VSPED"] = VSPED;
            Insert(dwData);
            vwMaster.View.MoveCurrentToFirst();
            IsNew = false;
            SART = "";
            VSPED = "";
            base.NewCommand(sender, e);
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
             
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            base.SaveCommand(sender, e);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DeleteVERSART(dwMaster, 1);
              filter = "VVERSART='" + dwMaster["VVERSART"].ToString() + "' AND VSPED='" + dwMaster["VSPED"].ToString() + "'";
            vwMaster.View.MoveCurrentTo(-1);
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
        }
        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["VMANDANT"] = app.Mandant;
            dwData["VVERSART"] = ORDERNUM.Substring(0,3);
            dwData["VSPED"] = ORDERNUM.Substring(3, 3);
            SResult = ((VVERSART)MV).QueryVERSART(dwData, 1);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
    }
}
